package com.ruoyi.play.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.play.domain.PlayAll;
import com.ruoyi.play.domain.PlayCopyright;
import com.ruoyi.play.mapper.PlayAllMapper;
import com.ruoyi.play.mapper.PlayCopyrightMapper;
import com.ruoyi.play.service.IPlayAllService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 短剧片单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-20
 */
@Service
public class PlayAllServiceImpl implements IPlayAllService {
    @Resource
    private PlayAllMapper playAllMapper;

    /**
     * 查询短剧片单
     *
     * @param id 短剧片单主键
     * @return 短剧片单
     */
    @Override
    public PlayAll selectPlayAllById(Long id)
    {
        return playAllMapper.selectPlayAllById(id);
    }

    /**
     * 查询短剧片单列表
     * 
     * @param playAll 短剧片单
     * @return 短剧片单
     */
    @Override
    public List<PlayAll> selectPlayAllList(PlayAll playAll)    {
        return playAllMapper.selectPlayAllList(playAll);
    }

    /**
     * 新增短剧片单
     *
     * @param playAll 短剧片单
     * @return 结果
     */
    @Override
    public int insertPlayAll(PlayAll playAll) {
        //新增时版权ID为空
        playAll.setCoId(null);
        playAll.setCreateTime(DateUtils.getNowDate());
        return playAllMapper.insertPlayAll(playAll);
    }

    @Resource
    PlayCopyrightMapper playCopyrightMapper;

    /**
     * 修改短剧片单
     *
     * @return 结果
     */
    @Override
    public AjaxResult updatePlayAll(PlayAll params) {
        PlayAll all = playAllMapper.selectPlayAllById(params.getId());
        if (all == null) {
            return AjaxResult.error("数据不存在！");
        }
        //版权ID不为空，数据更新
        if (params.getCoId() != null) {
            if (all.getCoId() == null) {
                //旧数据为空，直接更新
                PlayCopyright copy = new PlayCopyright();
                //版权ID不为空，版权那边数据也同时更新
                copy.setPlayId(params.getId());
                copy.setId(params.getCoId());
                playCopyrightMapper.updatePlayCopyright(copy);
            }
            if (all.getCoId() != null && all.getCoId() != params.getCoId()) {
                //旧数据不为空，先清除旧数据，再更新新数据
                playCopyrightMapper.updateOldPlayCopyright(all.getCoId());

                PlayCopyright copy = new PlayCopyright();
                //版权ID不为空，版权那边数据也同时更新
                copy.setId(params.getCoId());
                copy.setPlayId(params.getId());
                playCopyrightMapper.updatePlayCopyright(copy);
            }

        }


        int i = playAllMapper.updatePlayAll(params);

        return AjaxResult.success("修改成功！");
    }

    /**
     * 批量删除短剧片单
     * 
     * @param ids 需要删除的短剧片单主键
     * @return 结果
     */
    @Override
    public int deletePlayAllByIds(Long[] ids)
    {
        return playAllMapper.deletePlayAllByIds(ids);
    }

    /**
     * 删除短剧片单信息
     * 
     * @param id 短剧片单主键
     * @return 结果
     */
    @Override
    public int deletePlayAllById(Long id)
    {
        return playAllMapper.deletePlayAllById(id);
    }
}
